--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 主界面，从scene来，用于组织所有的layer，层级等等。




PlayGameData = class("PlayGameData",function()
    -- 继承
    local ret = {}

    ret.ary_data = nil

    return ret
end)
PlayGameData.__index = PlayGameData
PlayGameData.__instance = nil



function PlayGameData:getInstance()
    if(PlayGameData.__instance==nil)then
        PlayGameData.__instance = PlayGameData:create()
    end
    return PlayGameData.__instance
end

function PlayGameData:create()
    local ret = PlayGameData:new()
    ret:testInit()
    return ret
end




--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function PlayGameData:init(tab)
    -- 
    self:preSave()
    --
    self:readData( tab )
end



-- 测试的初始化，写死的
function PlayGameData:testInit()
    -- 
    self:preSave()
    --
    local dataTab = self:loadSaveData()
    -- 
    self:readData( dataTab )
end



function PlayGameData:readData(dataTab)

    -- 配置
    self.oneAdd = 9;            -- 每次叠加之后加分。
    self.oneKillScore = 17;     -- 每次消除1个0后加分。
    self.oneLineScore = 32;     -- 每次连排消0之后得分。
    -- 每次消除1个0后加分。
    self.birthLevelScore = {[1]=0, [2]=50, [3]=200, [4]=1000, 
                            [5]=3000, [6]=10000 }
    -- 每次消除1个0后加分。
    self.birthLevelTab = {  [1]={ 5 }, 
                            [2]={ 4,6 }, 
                            [3]={ 2,4,6,8 }, 
                            [4]={ 2,3,5,7 },
                            [5]={ 1,3,4,7,9 },
                            [6]={ 1,3,4,5,7,8,9 }
                        };  

    -- 状态初试化 
    self.step = 0;
    self.score = 0;
    self.birthLev = 1
    self.firstNum = nil
    self.lastNum = nil
    self.birthNumTab = self.birthLevelTab[1]    -- 正在使用的

    -- 初始化后，保存。
    self:saveDataLocal()
end




--------------------------------------------------------
-- 动态计算
--------------------------------------------------------
function PlayGameData:addOne()
    self.score = self.score + self.oneAdd
    self.firstNum = nil
    self.lastNum = nil
    --
    self:refreshData()
end


function PlayGameData:addOneZero()
    self.score = self.score + self.oneKillScore
    self.firstNum = nil
    self.lastNum = nil
    -- 
    self:refreshData()
end


function PlayGameData:addOneLineZero( num )
    self.score = self.score + (self.oneLineScore*num*num)
    --
    self:refreshData()
end


--------------------------------------------------------
-- 动态更新
--------------------------------------------------------
function PlayGameData:refreshData(  )
    self:refreshLevel()
end


-- 如果分达到了某个级别，下次随机出现的数字就从中随机。
function PlayGameData:refreshLevel(  )
    local max = getTableCount( self.birthLevelTab )
    for i=1, max do 
        if( self.score >= self.birthLevelScore[i] ) then
            self.birthLev = i
            self.birthNumTab = self.birthLevelTab[i]
        end
    end
    -- cclook(" now lev : %s", self.birthLev )
    -- cclook(" 使用的随机数数组: %s", self.birthNumTab )
end




--------------------------------------------------------
-- 初始化 plist 
--------------------------------------------------------
local path = cc.FileUtils:getInstance():getWritablePath().."/save/"
local file = path.."write.plist"

function PlayGameData:preSave()
    cclook(" 文件的保存路径 %s ", path )
    -- 
    local tab = { }
    tab["saveData"] = 1
    -- 创建目录
    if( cc.FileUtils:getInstance():isDirectoryExist(path)==false )then
        cclog(" 创建文件")
        local bo = cc.FileUtils:getInstance():createDirectory( path )
        cclook(" 创建文件 %s ", bo)
    else
        cclog("文件存在")
    end
    -- 写文件。
    if ( cc.FileUtils:getInstance():isFileExist(file)==false ) then 
        local wrt = cc.FileUtils:getInstance():writeToFile( tab, file )
        cclook("写 plist 文件 %s ",  wrt)
    end
end



function PlayGameData:loadSaveData()
    local tableSee = cc.FileUtils:getInstance():getValueMapFromFile( file )
    cclook(" 解析出来的key－value %s ", tableSee )
    return tableSee
end



function PlayGameData:saveDataLocal()
    local tab = { }
    if true then 
        tab.oneAdd = self.oneAdd               -- 
        tab.oneKillScore = self.oneKillScore    --
        tab.oneLineScore = self.oneLineScore    --
        tab.birthLevelScore = self.birthLevelScore
        tab.birthLevelTab = self.birthLevelTab    
    end
    local wrt = cc.FileUtils:getInstance():writeToFile( tab, file )
    cclook(" 更新 PlayGameData --- write.plist 文件 %s ",  wrt)
end


















